﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace card
{
    public partial class CycleCLLRForm : Form
    {
        public static WebDG.Service ws = new card.WebDG.Service();

        private TruckNoBox _truckNoBox;

        public CycleCLLRForm()
        {
            InitializeComponent();
        }

        private void CycleCLLRForm_Load(object sender, EventArgs e)
        {
            ComBind();
            LoadStack();
        }

        private void ComBind()
        {
            string strSQL = "select distinct STACKNAME from TRUCK_CARGO_LOGIN where TRUCKSTATUS <> '9' and STACKNAME is not null order by STACKNAME";
            cbbStackName.DataSource = ws.getDataSet_Serv(strSQL).Tables[0];
        }

        private void LoadStack()
        {
            DataSet ds = ws.getDataSet_Serv(" select ID, STACKNAME from TRUCK_CYCLE_STACK where ISDEL = '0' order by STACKNAME");
            this.dataGridViewStack.AutoGenerateColumns = false;
            this.dataGridViewStack.DataSource = ds.Tables[0];
        }

        private void LoadTruckMX()
        {
            if (this.dataGridViewStack.SelectedRows != null && this.dataGridViewStack.SelectedRows.Count > 0)
            {
                string strStackID = this.dataGridViewStack.SelectedRows[0].Cells["ID"].Value.ToString();
                string strSQL = "select ID, TRUCKNO, SFTIME from TRUCK_CYCLE_STACK_MX where STACKID = {0} and ISDEL = '0' order by TRUCKNO";
                DataSet ds = ws.getDataSet_Serv(string.Format(strSQL, strStackID));
                this.dataGridViewTruck.AutoGenerateColumns = false;
                this.dataGridViewTruck.DataSource = ds.Tables[0];
            }
            else
            {
                this.dataGridViewTruck.DataSource = null;
            }
        }

        private void dataGridViewStack_SelectionChanged(object sender, EventArgs e)
        {
            if (this.dataGridViewStack.SelectedRows != null)
            {
                LoadTruckMX();
            }
        }

        private void toolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            LoadStack();
        }

        private void toolStripButtonHistory_Click(object sender, EventArgs e)
        {
            CycleCLHistoryForm oForm = new CycleCLHistoryForm();
            oForm.Show();
        }

        private void btnAddStack_Click(object sender, EventArgs e)
        {
            string strStackName = this.cbbStackName.Text.Trim();
            if (string.IsNullOrEmpty(strStackName))
            {
                MessageBox.Show("请选择垛名！");
                return;
            }
            DataTable dtStack = ws.getDataSet_Serv("select ID, ISDEL from TRUCK_CYCLE_STACK where STACKNAME = '" + strStackName + "'").Tables[0];
            if (dtStack != null && dtStack.Rows.Count > 0)
            {
                if (dtStack.Rows[0]["ISDEL"].ToString() == "1")
                {
                    ws.execute_UID_Serv("update TRUCK_CYCLE_STACK set ISDEL='0',ADDMAN='" + Session.Operator_User + "',ADDTIME=SYSDATE where ID=" + dtStack.Rows[0]["ID"].ToString());
                    MessageBox.Show("添加成功！");
                    LoadStack();
                }
                else
                {
                    MessageBox.Show("该垛名已存在！");
                }
            }
            else//新增
            {
                string strID = create_id();
                string strSQL = "insert into TRUCK_CYCLE_STACK(ID,STACKNAME,ADDMAN,ADDTIME,ISDEL) values({0}, '{1}', '{2}', sysdate, '0')";
                ws.execute_UID_Serv(string.Format(strSQL, strID, strStackName, Session.Operator_User));
                MessageBox.Show("添加成功！");
                LoadStack();
            }
        }

        private void btnDeleteStack_Click(object sender, EventArgs e)
        {
            if (this.dataGridViewStack.SelectedRows != null && this.dataGridViewStack.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in this.dataGridViewStack.SelectedRows)
                {
                    string strID = row.Cells["ID"].Value.ToString();
                    ws.execute_UID_Serv("update TRUCK_CYCLE_STACK_MX set ISDEL='1',SFTIME=null,DELMAN='" + Session.Operator_User + "',DELTIME=SYSDATE where STACKID=" + strID);
                    ws.execute_UID_Serv("update TRUCK_CYCLE_STACK set ISDEL='1',DELMAN='" + Session.Operator_User + "',DELTIME=SYSDATE where ID = " + strID);
                }
                MessageBox.Show("删除成功！");
                LoadStack();
            }
        }

        private void btnAddTruckNo_Click(object sender, EventArgs e)
        {
            if (this.dataGridViewStack.SelectedRows != null && this.dataGridViewStack.SelectedRows.Count > 0)
            {
                string strTruckNo = this.tbxTruckNo.Text.Trim();
                if (strTruckNo.Length != 7)
                {
                    MessageBox.Show("车号位数不正确！");
                    return;
                }
                DataGridViewRow row = this.dataGridViewStack.SelectedRows[0];
                string strStactID = row.Cells["ID"].Value.ToString();
                DataTable dtTruck = ws.getDataSet_Serv("select ID,ISDEL from TRUCK_CYCLE_STACK_MX where STACKID=" + strStactID + " and TRUCKNO='" + strTruckNo + "'").Tables[0];
                if (dtTruck != null && dtTruck.Rows.Count > 0)
                {
                    if (dtTruck.Rows[0]["ISDEL"].ToString() == "1")
                    {
                        ws.execute_UID_Serv("update TRUCK_CYCLE_STACK_MX set ISDEL='0',SFTIME=null,ADDMAN='" + Session.Operator_User + "',ADDTIME=SYSDATE where ID=" + dtTruck.Rows[0]["ID"].ToString());
                        MessageBox.Show("添加成功！");
                        LoadTruckMX();
                    }
                    else
                    {
                        MessageBox.Show("该车号已存在！");
                    }
                }
                else//新增
                {
                    string strStackName = row.Cells["STACKNAME"].Value.ToString();
                    string strID = create_id();
                    string strSQL = "insert into TRUCK_CYCLE_STACK_MX(ID,STACKID,STACKNAME,TRUCKNO,ADDMAN,ADDTIME,ISDEL) values({0},{1},'{2}','{3}','{4}',sysdate,'0')";
                    ws.execute_UID_Serv(string.Format(strSQL, strID, strStactID, strStackName, strTruckNo, Session.Operator_User));
                    MessageBox.Show("添加成功！");
                    LoadTruckMX();
                }
            }
        }

        private void btnDeleteTruckNo_Click(object sender, EventArgs e)
        {
            if (this.dataGridViewTruck.SelectedRows != null && this.dataGridViewTruck.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in this.dataGridViewTruck.SelectedRows)
                {
                    string strID = row.Cells["MXID"].Value.ToString();
                    ws.execute_UID_Serv("update TRUCK_CYCLE_STACK_MX set ISDEL='1',SFTIME=null,DELMAN='" + Session.Operator_User + "',DELTIME=SYSDATE where ID=" + strID);
                }
                MessageBox.Show("删除成功！");
                LoadTruckMX();
            }
        }

        private string create_id()
        {
            string sequence = "";
            string sqlStr = "select SEQ_TRUCK.nextval from dual";
            DataSet ds1 = ws.getDataSet_Serv(sqlStr);
            if (ds1.Tables[0].Rows.Count > 0)
            {
                sequence = ds1.Tables[0].Rows[0][0].ToString();
            }
            string year_str = DateTime.Now.Year.ToString();
            string month_str = DateTime.Now.Month.ToString();
            string day_str = DateTime.Now.Day.ToString();
            string id = year_str + month_str + day_str + sequence;
            return id;
        }

        private void CycleCLLRForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                if (_truckNoBox != null && _truckNoBox.Visible)
                {
                    _truckNoBox.Visible = false;
                }
                else
                {
                    btnF1.PerformClick();
                }
            }
        }

        private void btnF1_Click(object sender, EventArgs e)
        {
            if (_truckNoBox == null)
            {
                _truckNoBox = new TruckNoBox(this.tbxTruckNo);
                _truckNoBox.Parent = this.splitMain.Panel2;
                _truckNoBox.Top = tbxTruckNo.Top + tbxTruckNo.Size.Height;
                _truckNoBox.Left = tbxTruckNo.Left;
                _truckNoBox.BringToFront();
            }
            _truckNoBox.Visible = true;
            this.tbxTruckNo.SelectionStart = this.tbxTruckNo.Text.Length;
            this.tbxTruckNo.Focus();
        }
    }
}
